

package com.pig4cloud.pig.common.data.enums;

import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.admin.api.entity.SysRole;
import com.pig4cloud.pig.common.security.service.PigUser;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lengleng
 * @date 2018/12/26
 * <p>
 * 数据权限类型
 */
@Getter
@AllArgsConstructor
public enum DataScopeTypeEnum {
    /**
     * 查询全部数据
     */
    ALL( "全部",1) {
        @Override
        public Boolean calcScope(List<Integer> list, List<Integer> deptIdList, SysRole role, PigUser user) {
            return true;
        }
    },

    /**
     * 自定义
     */
    CUSTOM( "自定义",1) {
        @Override
        public Boolean calcScope(List<Integer> list, List<Integer> deptIdList, SysRole role, PigUser user) {
            String dsScope = role.getDsScope();
            list.addAll(Arrays.stream(dsScope.split(StrUtil.COMMA)).map(Integer::parseInt).collect(Collectors.toList()));
            return false;
        }
    },

    /**
     * 本级及子级
     */
    OWN_CHILD_LEVEL( "本级及子级",2) {
        @Override
        public Boolean calcScope(List<Integer> list, List<Integer> deptIdList, SysRole role, PigUser user) {
            list.addAll(deptIdList);
            return false;
        }
    },

    /**
     * 本级
     */
    OWN_LEVEL("本级",3){
        @Override
        public Boolean calcScope(List<Integer> list, List<Integer> deptIdList, SysRole role, PigUser user) {
            list.add(user.getDeptId());
            return false;
        }
    };

    /**
     * 描述
     */
    private final String description;
    /**
     * 类型
     */
    private final int type;


    public abstract Boolean calcScope(List<Integer> list, List<Integer> deptIdList, SysRole role, PigUser user);
}
